#include "ComplexNumber.h"


ComplexNumber::ComplexNumber(void)
{
}

ComplexNumber::ComplexNumber(double real, double imag)
{
	this->real = real;
	this->imag = imag;
}

ComplexNumber::~ComplexNumber(void)
{
}

ComplexNumber& ComplexNumber::operator+(ComplexNumber &b)
{
	ComplexNumber& result = *new ComplexNumber(0, 0);
	result.real = this->real + b.real;
	result.imag = this->imag + b.imag;
	return result;
}
	
ComplexNumber& ComplexNumber::operator-(ComplexNumber &b)
{
	ComplexNumber& result = *new ComplexNumber(0, 0);
	result.real = this->real - b.real;
	result.imag = this->imag - b.imag;
	return result;
}
	
ComplexNumber& ComplexNumber::operator*(ComplexNumber &b)
{
	ComplexNumber& result = *new ComplexNumber(0, 0);
	result.real = this->real * b.real - this->imag * b.imag;
	result.imag = this->real * b.imag + this->imag * b.real;
	return result;
}
	
ComplexNumber& ComplexNumber::operator/(ComplexNumber &b)
{
	ComplexNumber& result = *new ComplexNumber(0, 0);
	result.real = (this->real * b.real + this->imag * b.imag) / (b.real * b.real + b.imag * b.imag);
	result.imag = (this->imag * b.imag - this->real * b.imag) / (b.real * b.real + b.imag * b.imag);
	return result;
}
	
ComplexNumber& ComplexNumber::Sqrt()
{
	ComplexNumber &result = *new ComplexNumber();
	result = this->ToPolar();
	result.real = sqrt(result.real);
	result.imag = result.imag/2;
	return result;
}
	
bool ComplexNumber::operator==(ComplexNumber &b)
{
	if(this->real == b.real && this->imag == b.imag)
		return true;
	return false;
}
	
bool ComplexNumber::operator!=(ComplexNumber &b)
{
	if(this->real == b.real && this->imag == b.imag)
		return false;
	return true;
}
	
ComplexNumber& ComplexNumber::Conj()
{
	ComplexNumber &result = *new ComplexNumber();
	result.real = this->real;
	result.imag = this->imag * -1;
	return result;
}
	
double ComplexNumber::Abs()
{
	return sqrt(this->real * this->real + this->imag * this->imag);
}
	
ComplexNumber& ComplexNumber::ToPolar()
{
	//real = betrag
	//imag = winkel
	ComplexNumber &result = *new ComplexNumber();
	result.real = this->Abs();

	if(this->real >= 0 && this->imag >= 0)
	{
		result.imag = asin(this->imag/result.real) * 180 / PI;
		return result;
	}
	else if(this->real < 0 && this->imag >= 0)
	{
		result.imag = 180 - asin(this->imag/result.real) * 180 /PI;
		return result;
	}
	else if(this->real < 0 && this->imag < 0)
	{
		result.imag = 180 + asin((-1*this->imag)/result.real) * 180 /PI;
		return result;
	}
	else if(this->real >= 0 && this->imag < 0)
	{
		result.imag = asin(this->imag/result.real) * 180 / PI;
		return result;
	}

}
